<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" @keyup.enter.native="search()">
      <el-form-item label="礼品编码"> <br> <br><el-input v-model="formInline.giftCode" placeholder clearable style="margin-left:-60px;width:200px"></el-input></el-form-item>
      <el-form-item label="礼品名称"> <br> <br><el-input v-model="formInline.giftName" placeholder clearable style="margin-left:-60px;width:200px"></el-input></el-form-item>
      <el-form-item label="礼品类型">
        <br> <br>
        <el-select v-model="formInline.giftType" clearable style="margin-left:-60px;width:200px">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left:700px;text-align:right"><el-button type="primary" @click="search()" size="mini" icon="el-icon-search">查询</el-button>
        <el-button type="primary" @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
        <el-button type="primary" @click="toAdd()" icon="el-icon-edit" size="mini">新增</el-button></el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      border
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      v-loading="loading"
    >
      <el-table-column type="index" label="序号" align="center" width="60" ></el-table-column>

      <el-table-column prop="giftName" label="礼品名称" width align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="giftCode" label="礼品编码" width align="left"></el-table-column>
      <el-table-column align="center" prop="giftMainPic" label="礼品主图" width>
        <template slot-scope="scope">
          <img
            :src="scope.row.giftMainPic"
            v-if="scope.row.giftMainPic != '' && scope.row.giftMainPic != null"
            width="70"
            height="50"
          />
        </template>
      </el-table-column>
      <el-table-column prop="giftType" label="礼品类型" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.giftType == '1'">实物礼品</span>
          <span v-if="scope.row.giftType == '2'">虚拟礼品</span>

        </template>
      </el-table-column>

      <el-table-column prop="giftDesc" label="礼品详情" width align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width align="left" show-overflow-tooltip></el-table-column>
      <!-- <el-table :data="tableData" border row-key="id" align="left"    :header-cell-style="{ background: '#eef1f6', color: '#606266' }"> -->
      <!-- <el-table-column type="index" label="序号" align="center" width="60" fixed></el-table-column>  -->
      <!-- <el-table-column
                      width="60px"
                      label="序号"
                      type="index">
                </el-table-column> -->
      <!-- <el-table-column v-for="(item, index) in col" :key="`col_${index}`" :prop="dropCol[index].prop" :width="dropCol[index].width" :label="item.label">
        <template slot-scope="scope">
          <img
            :src="scope.row[dropCol[index].prop]"
            v-if="dropCol[index].prop === 'prizePicture'&&scope.row[dropCol[index].prop]"
            width="70"
            height="50"
          />
          <span v-if="dropCol[index].prop !== 'prizePicture'">{{ dropCol[index].prop==='prizeType'?(scope.row[dropCol[index].prop]===1?"虚拟奖品" : "实物奖品"):scope.row[dropCol[index].prop] }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="seeEdit(scope.row)">查看</el-button>
          <el-button type="text" size="mini" @click="toEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="handledelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <pre style="text-align: left">
      {{dropCol}}
    </pre>
    <hr>
    <pre style="text-align: left">
      {{tableData}}
    </pre> -->
    <!-- </el-table> -->
    <el-form :inline="true" :model="formInline"><el-form-item style="margin-top: 6px;"></el-form-item></el-form>
    <div class="block" style="margin-top:-49px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50, 100]"
        :current-page="listQuery.current"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog :visible.sync="dialogVisibleImg"><img width="100%" :src="dialogImageUrl" alt /></el-dialog>
  </div>
</template>

<script>
  import Sortable from 'sortablejs';
  // import { queryPrize, addPrize, deletePrize, updatePrize } from '@/api/prizeLibrary';

  import { queryPrize, addPrize, deletePrize, updatePrize } from '@/api/pointsGiftBank';
  import imageUpload from '@/page/common/Image-upload';
  import qs from 'qs';
  export default {
    data() {
      return {
        tableData: [],
        loading: true,
        disabled: false,
        flag: true,
        col: [
          // {
          //   label: '序号',
          //   prop: 'index'
          // },
          {
            label: '奖品编码',
            prop: 'prizeCode',

          },
          {
            label: '奖品名称',
            prop: 'prizeName'
          },
          {
            label: '奖品描述',
            prop: 'prizeInfo'
          },
          {
            label: '奖品图片',
            prop: 'prizePicture'
          },
          {
            label: '奖品类型',
            prop: 'prizeType'
          }
        ],
        dropCol: [
          // {
          //     label: '序号',
          //     prop: 'index'
          //   },
          {
            label: '奖品编码',
            prop: 'prizeCode',

          },
          {
            label: '奖品名称',
            prop: 'prizeName'
          },
          {
            label: '奖品描述',
            prop: 'prizeInfo'
          },
          {
            label: '奖品图片',
            prop: 'prizePicture'
          },
          {
            label: '奖品类型',
            prop: 'prizeType'
          }
        ],
        typeOptions: [ { value: '1', label: '实物礼品' }],
        total: 0,
        listQuery: {
          current: 1,
          size: 100
        },
        dialogVisibleImg: false,
        dialogImageUrl: '',
        formInline: {
          giftCode: '',
          giftName: '',
          giftType: ''
        },
        rules: {}
      };
    },
    mounted() {
      this.getDataList();
      document.body.ondrop = function(event) {
        event.preventDefault();
        event.stopPropagation();
      };
      // this.rowDrop()
      // this.columnDrop();
    },
    components: {
      imageUpload
    },
    created() {},
    methods: {
      // columnDrop() {
      //   const wrapperTr = document.querySelector('.el-table__header-wrapper tr');
      //   this.sortable = Sortable.create(wrapperTr, {
      //     animation: 180,
      //     delay: 0,
      //     onEnd: evt => {
      //       const oldItem = this.dropCol[evt.oldIndex];
      //       this.dropCol.splice(evt.oldIndex, 1);
      //       this.dropCol.splice(evt.newIndex, 0, oldItem);
      //     }
      //   });
      // },

      kong() {
        this.formInline.giftCode = '';
        this.formInline.giftName = '';
        this.formInline.giftType = '';
      },
      search() {
        this.loading = true;
        this.listQuery.current = 1;
        let data = {
          size: this.listQuery.size,
          current: this.listQuery.current,
          condition: {
            giftCode: this.formInline.giftCode,
            giftName: this.formInline.giftName,
            giftType: this.formInline.giftType,
            status: 1
          }
        };
        queryPrize(data).then(res => {
          this.loading = false;
          this.total = res.total;
          this.tableData = res.records;
        });
      },
      handledelete(v) {
        let data = {
          uuid: v.uuid,
          status:"0",
        };
        this.$confirm('确认删除吗？').then(_ => {
          deletePrize(data).then(res => {
            this.$message({
              showClose: true,
              message: res.message,
              type: res.code == 1 ? 'success' : 'error'
            });
            this.getDataList();
          });
        });
      },
      toAdd() {
        this.$router.push({
          name: 'pointsGiftBankEdit',
          params: {}
        });
      },
      toEdit(item) {
        this.$router.push({
          name: 'pointsGiftBankEdit',
          params: {
            edit: item,
            bukan:false,
          }
        });
      },
      seeEdit(item) {
        this.$router.push({
          name: 'pointsGiftBankEdit',
          params: {
            edit: item,
            bukan:true,
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.size = val;
        this.getDataList();
      },
      handleCurrentChange(val) {
        this.listQuery.current = val;
        this.getDataList();
      },

      getDataList() {
        let data = {
          size: this.listQuery.size,
          current: this.listQuery.current,
          condition: {
            status: 1
          }
        };
        queryPrize(data).then(res => {
          this.loading = false;
          this.total = res.total;
          this.tableData = res.records;
        });
      }
    }
  };
</script>
<style lang="less" scoped>
  .el-form-item .el-select {
    width: 100%;
  }
</style>
